package scau.jump.securitytrace.dataModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.table.DefaultTableModel;

import scau.jump.securitytrace.util.ConvertAdapter;
import scau.jump.securitytrace.util.UserConvertToVector;

import com.securitytrace.model.User;

public class UserDataModel extends DefaultTableModel {

	private static final String[] userColumns = new String[] { "账号", "密码",
			"权限", "工作地点" };
	private static final long serialVersionUID = 6285441594259688626L;
	@SuppressWarnings("rawtypes")
	private Class[] types = new Class[] { java.lang.String.class,
		java.lang.String.class, java.lang.String.class,
		java.lang.String.class };
	private boolean[] canEdit = new boolean[] { false, false, false, false };

	private Map<Integer, User> userMap = new HashMap<Integer, User>();
	private int userCurrentPage;
	private int userTotal;
	private ConvertAdapter<User> userConvertToVector;

	public UserDataModel() {
		userConvertToVector = new UserConvertToVector();
	}

	/** 以下方法是setDataVector,是对model初始化数据使用 */
	public void setDataVector(List<User> users) {
		for (int row = 0; row < users.size(); row++) {
			userMap.put(row, users.get(row));
		}
		super.setDataVector(userConvertToVector.convertToVector(users),
				userConvertToVector.convertToVector(userColumns));
	}

	/** 以下方法是get 一行数据 **/
	public User getRow(int row) {
		return userMap.get(row);
	}

	/** 以下方法是更新一行数据 **/
	public void setValueAtRow(User user, int row) {
		userMap.put(row, user);
		this.setValueAt(user.getAccount(), row, 0);
		this.setValueAt(user.getPassword(), row, 1);
		this.setValueAt(user.getRole().getName(), row, 2);
		this.setValueAt(user.getAddress().getAddressName(), row, 3);
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return canEdit[columnIndex];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return types[columnIndex];
	}

	/**
	 * 以下为get/set方法
	 */
	public Map<Integer, User> getUserMap() {
		return userMap;
	}

	public void setUserMap(Map<Integer, User> userMap) {
		this.userMap = userMap;
	}
	public int getUserCurrentPage() {
		if(0 < userCurrentPage) {
			return userCurrentPage;
		}else {
			return 1;
		}
	}
	public void setUserCurrentPage(int userCurrentPage) {
		this.userCurrentPage = userCurrentPage;
	}
	public int getUserTotal() {
		if(userTotal > 0) {
			return userTotal;
		}else {
			return 1;
		}
	}
	public void setUserTotal(int userTotal) {
		this.userTotal = userTotal;
	}
	public String getUserPageInfo() {
		return "共 " + this.getUserTotal() +" 页，当前为第 "+this.getUserCurrentPage()+" 页";
	}
}
